package gameEngine;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 * @author Joe R. Nassimian
 * @author Yannis Mazzer
 */
public class PiecesList extends PlayerSon {
	private Hashtable<Integer,ArrayList<Piece>> pieces;
	//private int nextline;
	//private Player parent;
	private int edgeSize;

	public PiecesList(Player _parent){
		super(_parent);
		pieces = new Hashtable<Integer,ArrayList<Piece>>();
		
		pieces.put(1,buildMonomino());  //  1 piece de 1 sous-piece = 01;
		pieces.put(2,buildDomino());  //  1 piece de 2 sous-pieces = 02;
		pieces.put(3,buildTriomino());  //  2 pieces de 3 sous-pieces = 06;
		pieces.put(4,buildTetramino());  //  5 pieces de 4 sous-pieces = 20;
		pieces.put(5,buildPentamino()); // 12 pieces de 5 sous-pieces = 60;
	}
	
	private ArrayList<Piece> buildMonomino(){
		ArrayList<Piece> arrayMonomino;
		int[][] config = {{0}}; // Monomino's configuration code
		
		arrayMonomino = new ArrayList<Piece>(); // need to specifically create monominos
		arrayMonomino.add(new Piece(config, this.get親())); // I add the monomino piece to the array
		
		return arrayMonomino;
	}

	private ArrayList<Piece> buildDomino(){
		ArrayList<Piece> arrayDomino;
		int[][] config = {{0,1}};
		
		arrayDomino = new ArrayList<Piece>(); // need to specifically create Dominos
		arrayDomino.add(new Piece(config, this.get親())); // I add the Domino piece to the array
		
		return arrayDomino;
	}
	
	private ArrayList<Piece> buildTriomino(){
		ArrayList<Piece> arrayTriomino;
		int[][][] config = {
				{
					{0,1,2},
				},
				{
					{0,1},
					  {1}
				},
			};
		
		arrayTriomino = new ArrayList<Piece>(); // need to specifically create Triominos
		
		for(int i=0 ; i < config.length ; i++) {
		arrayTriomino.add(new Piece(config[i], this.get親())); // I add the Triomino piece to the array
		}
		
		return arrayTriomino;
	}
	
	private ArrayList<Piece> buildTetramino(){
		ArrayList<Piece> arrayTetramino;
		int[][][] config = {
							{
								{0,1},
								{0,1}
							},
							{
								{0,1,2},
								    {2}
							},
							{
								{0,1,2},
								  {1}
							},
							{
								{0},
								{0,1},
								  {1}
							},
							{
								{0,1,2,3}
							}
						   };
		
		arrayTetramino = new ArrayList<Piece>(); // need to specifically create Tetraminos
		
		for(int i=0 ; i < config.length ; i++) {
		arrayTetramino.add(new Piece(config[i], this.get親())); // I add the Tetramino piece to the array
		}
		
		return arrayTetramino;
	}

	private ArrayList<Piece> buildPentamino(){
		ArrayList<Piece> arrayPentamino;
		int[][][] config =  { {
								{0,1,2,3},
								      {4}
							  },
				              {
			                    {0,1,2},
			                      {1},
			                      {1}
				              },
							  {
								{0,1,2},
								{0},
								{0}
							  },
							  {
								{0},
								{0,1},
								  {1},
								  {1}
							  },
							  {
								{0,1},
								  {1},
								  {1,2}
							  },
							  {
								{0,1,2,3,4}
							  },
							  {
								{0,1,2},
								{0,1}
							  },
							  { 
								{0},
								{0,1},
								  {1,2}
							  },
							  {
								{0,1,2},
								{0,  2}
							  },
							  {
								{0},
								{0,1,2},
								  {1}
							  },
							  {								
								{0,1,2,3},
								    {2}, 
							  },
							  {
								  {1},
								{0,1,2},
								  {1}
								  
							  }
							};
		
		arrayPentamino = new ArrayList<Piece>(); // need to specifically create Pentaminos
		
		for(int i=0 ; i < config.length ; i++) {
		arrayPentamino.add(new Piece(config[i], this.get親())); // I add the Pentamino piece to the array
		}
		
		return arrayPentamino;
	}
	
	public int getNumberOfPieceTypes() {
		return pieces.size();
	}
	
	public int getNumberOfPieces () {
		/*
		int res = 0;
		for(int i = 0; i < pieces.size(); i++) {
			System.out.println(i);
			if(pieces.get(i) != null) {
				res += pieces.get(i).size();
			}
		}
		return res;
		*/
		return pieces.size();
	}
	
	public ArrayList<Piece> getPieces() {
		ArrayList<Piece> res = new ArrayList<Piece>();
		for(int i = 1; i < pieces.size(); i++) {
			for(int j = 1; j < pieces.get(i).size(); j++) {
				res.add(pieces.get(i).get(j));
			}
		}
		return res;
	}
	
	public Piece getPiece(int id) {
		if(pieces.get(id).isEmpty() == false) {
			return pieces.get(id).get(0);
		}
		else {
			return null;
		}
	}

	/**
	 * @param edgeSize the edgeSize to set
	 */
	public void setEdgeSize(int edgeSize) {
		this.edgeSize = edgeSize;
		setChanged();
		notifyObservers();
	}

	/**
	 * @return the edgeSize
	 */
	public int getEdgeSize() {
		return edgeSize;
	}
}
